package hmall.gateway.filter;

import cn.hutool.core.text.AntPathMatcher;
import hmall.gateway.config.AuthProperties;
import hmall.gateway.config.JwtProperties;
import hmall.gateway.utils.JwtTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

@EnableConfigurationProperties({JwtProperties.class,AuthProperties.class})
@Component
public class AuthGlobalFilter implements GlobalFilter {
    @Autowired
    private AuthProperties authProperties;
    private AntPathMatcher antPathMatcher = new AntPathMatcher();
    @Autowired
    private JwtTool jwtTool;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String uri = request.getPath().toString();
        boolean bo = authProperties.getExcludePaths()
                .stream()
                .anyMatch(ep -> antPathMatcher.match(ep, uri));
        if (bo) {
            return chain.filter(exchange);
        }
        String token = request.getHeaders().getFirst("authorization");
        Long userId = null;
        try {
            userId = jwtTool.parseToken(token);
        } catch (Exception e) {
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            return response.writeWith(Mono.just(response.bufferFactory().wrap("{\"msg\":\"请先登录！\"}".getBytes(StandardCharsets.UTF_8))));
        }
        String userInfo = userId.toString();
        exchange.mutate().request(builder -> builder.header("user-info",userInfo));
        return chain.filter(exchange);
    }
}
